package cn.com.surker.excel;

import cn.com.surker.util.StringUtils;
import cn.hutool.core.util.ObjectUtil;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * @Author lucky
 * @create 2023年10月19 10:32
 * @desc
 */
public class ExcelStyleUtils {

    /**
     * 返回单元格样式
     * @param wb
     * @param isVerticalCenter 是否垂直居中
     * @param isSolidForeground 是否固定
     * @param isAlignCenter 文字居中
     * @param isTop 上边框
     * @param isBottom 下边框
     * @param isLeft 左边框
     * @param isRight 右边框
     * @return
     */
    public static XSSFCellStyle cellStyle(XSSFWorkbook wb, Boolean isVerticalCenter, Boolean isSolidForeground, Boolean isAlignCenter,
                                          Boolean isTop, Boolean isBottom, Boolean isLeft, Boolean isRight, Short color) {
        XSSFCellStyle valueStyle = wb.createCellStyle();
        if (!StringUtils.isNull(isVerticalCenter) && isVerticalCenter)
            valueStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        if (!StringUtils.isNull(isSolidForeground) && isSolidForeground)
            valueStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        if (!StringUtils.isNull(isAlignCenter) && isAlignCenter)
            valueStyle.setAlignment(HorizontalAlignment.CENTER); // 居中
        if (!StringUtils.isNull(isBottom) && isBottom)
            valueStyle.setBorderBottom(BorderStyle.THIN); //下边框
        if (!StringUtils.isNull(isLeft) && isLeft)
            valueStyle.setBorderLeft(BorderStyle.THIN);//左边框
        if (!StringUtils.isNull(isTop) && isTop)
            valueStyle.setBorderTop(BorderStyle.THIN);//上边框
        if (!StringUtils.isNull(isRight) && isRight)
            valueStyle.setBorderRight(BorderStyle.THIN);//右边框
        if (StringUtils.isNull(color))
            color = IndexedColors.WHITE.index;
        valueStyle.setFillForegroundColor(color);
        return valueStyle;
    }

    /**
     * 返回单元格样式
     * @param wb
     * @param alignment 是否垂直居中
     * @param isSolidForeground 是否固定
     * @param isAlignCenter 文字居中
     * @param isTop 上边框
     * @param isBottom 下边框
     * @param isLeft 左边框
     * @param isRight 右边框
     * @return
     */
    public static XSSFCellStyle cellStyle(XSSFWorkbook wb, Boolean isVerticalCenter, Short alignment, Boolean isSolidForeground, Boolean isAlignCenter,
                                          Boolean isTop, Boolean isBottom, Boolean isLeft, Boolean isRight, Short color) {
        XSSFCellStyle valueStyle = wb.createCellStyle();
        if (!StringUtils.isNull(isVerticalCenter) && isVerticalCenter)
            valueStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
//        if (!StringUtils.isNull(alignment))
//            valueStyle.setAlignment(alignment);//垂直居中
        if (!StringUtils.isNull(isSolidForeground) && isSolidForeground)
            valueStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        if (!StringUtils.isNull(isAlignCenter) && isAlignCenter)
            valueStyle.setAlignment(HorizontalAlignment.CENTER); // 居中
        if (!StringUtils.isNull(isBottom) && isBottom)
            valueStyle.setBorderBottom(BorderStyle.THIN); //下边框
        if (!StringUtils.isNull(isLeft) && isLeft)
            valueStyle.setBorderLeft(BorderStyle.THIN);//左边框
        if (!StringUtils.isNull(isTop) && isTop)
            valueStyle.setBorderTop(BorderStyle.THIN);//上边框
        if (!StringUtils.isNull(isRight) && isRight)
            valueStyle.setBorderRight(BorderStyle.THIN);//右边框
        if (StringUtils.isNull(color))
            color = IndexedColors.WHITE.index;
        valueStyle.setFillForegroundColor(color);
        return valueStyle;
    }

    /**
     * 返回单元格样式
     * @param wb
     * @param verticalCenter 垂直居中
     * @param isSolidForeground 是否固定
     * @param alignCenter 文字居中
     * @param isTop 上边框
     * @param isBottom 下边框
     * @param isLeft 左边框
     * @param isRight 右边框
     * @return
     */
    public static XSSFCellStyle cellStyleCustomize(XSSFWorkbook wb, VerticalAlignment verticalCenter, Boolean isSolidForeground, HorizontalAlignment alignCenter,
                                          Boolean isTop, Boolean isBottom, Boolean isLeft, Boolean isRight, Short color) {
        XSSFCellStyle valueStyle = wb.createCellStyle();
        if (ObjectUtil.isEmpty(verticalCenter)){
            valueStyle.setVerticalAlignment(VerticalAlignment.CENTER);//垂直居中
        }else{
            valueStyle.setVerticalAlignment(verticalCenter);//垂直居中
        }
        if (!StringUtils.isNull(isSolidForeground) && isSolidForeground)
            valueStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        if (ObjectUtil.isEmpty(alignCenter)){
            valueStyle.setAlignment(HorizontalAlignment.CENTER); // 默认居中
        }else{
            valueStyle.setAlignment(alignCenter); // 自定义
        }
        if (!StringUtils.isNull(isBottom) && isBottom)
            valueStyle.setBorderBottom(BorderStyle.THIN); //下边框
        if (!StringUtils.isNull(isLeft) && isLeft)
            valueStyle.setBorderLeft(BorderStyle.THIN);//左边框
        if (!StringUtils.isNull(isTop) && isTop)
            valueStyle.setBorderTop(BorderStyle.THIN);//上边框
        if (!StringUtils.isNull(isRight) && isRight)
            valueStyle.setBorderRight(BorderStyle.THIN);//右边框
        if (StringUtils.isNull(color))
            color = IndexedColors.WHITE.index;
        valueStyle.setFillForegroundColor(color);
        return valueStyle;
    }

    public static XSSFFont hssfFont(XSSFWorkbook wb, Integer fontSize, String fontName, Short color, Boolean bold) {
        XSSFFont font = wb.createFont();
        if (StringUtils.isNull(fontSize))
            fontSize = 10;
        font.setFontHeightInPoints(fontSize.shortValue());//设置字体大小
        if (StringUtils.isNull(fontName))
            fontName = "宋体";
        font.setFontName(fontName);
        if (StringUtils.isNull(color))
            color = IndexedColors.BLACK.index;
        if (!StringUtils.isNull(bold))
            font.setBold(bold);
        font.setColor(color);

        return font;
    }

}
